<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 19/9/23
 * Time: 16:24
 */

namespace app\store\controller;


use app\common\bean\StorePermissionBean;
use app\common\controller\StoreController;
use app\common\manage\StorePermissionManage;
use app\common\manage\StoreRolePermissionManage;
use app\common\utils\ExitJsonUtil;

class StorePermission extends StoreController
{
    /** 权限列表
     * @return mixed
     */
    public function permissionList(){
        list($permissionTree,$permissionCount) = StoreRolePermissionManage::getInstance()->getAllPermissionTree();
        $this->assign("permissionTree",json_encode($permissionTree));
        $this->assign("totalCount",$permissionCount);
        return $this->fetch();
    }

    /**添加顶级权限节点
     * @return mixed
     * @throws \Exception
     */
    public function addTopPermission(){
        if(request()->isPost()){
            $storePermission = new StorePermissionBean();
            $storePermission->setData($this->postParams);
            $result = StorePermissionManage::getInstance()->addPermissonNode($storePermission,true);
            ExitJsonUtil::getInstance()->exitData($result);
        }
        $this->assign("parentPermissionDetail",false);
        return $this->fetch("add_permission");
    }

    /**添加子权限节点
     * @return mixed
     */
    public function addChildPermission(){
        $storePermission = new StorePermissionBean();
        $storePermissionManage = StorePermissionManage::getInstance();
        if(request()->isAjax() && request()->isPost()){
            $storePermission->setData($this->postParams);
            $resdutl = $storePermissionManage->addPermissonNode($storePermission);
            ExitJsonUtil::getInstance()->exitData($resdutl);
        }
        $storePermission->setData($this->getParams);
        $storePermission = $storePermissionManage->getPermissionById($storePermission);
        $this->assign("parentPermissionDetail",$storePermission->getData());
        return $this->fetch("add_permission");
    }

    /**
     * 编辑权限节点
     * @throws \Exception
     */
    public function editPermission(){
        $storePermission = new StorePermissionBean();
        $storePermissionManage = StorePermissionManage::getInstance();
        if(request()->isAjax() && request()->isPost()){
            $storePermission->setData($this->postParams);
            $result = $storePermissionManage->updatePermission($storePermission);
            ExitJsonUtil::getInstance()->exitData($result);
        }
        $storePermission->setData($this->getParams);
        $storePermission = $storePermissionManage->getPermissionById($storePermission);
        $parentPermissionDetail = $storePermissionManage->getParentPermission($storePermission);
        $this->assign("permissionDetail",$storePermission->getData());
        $this->assign("parentPermissionDetail",$parentPermissionDetail->getData());
        return $this->fetch();
    }

    /**
     * 删除权限节点
     */
    public function delPermission(){
        $storePermission = new StorePermissionBean();
        $storePermission->setData($this->postParams);
        $result = StorePermissionManage::getInstance()->delPermissionById($storePermission);
        ExitJsonUtil::getInstance()->exitData($result);
    }
}